package java复习202407;

import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

public class c10_多线程验证 {

    @Test
    public void 三种线程的创建方式() throws ExecutionException, InterruptedException {
        // 继承Thread类，重新run方法
        new Thread(){
            @Override
            public void run() {
                System.out.println("Thread线程执行。。。。。。。");
                System.out.println(Thread.currentThread());
            }
        }.start();

        // 实现runnable接口
        new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("Runnable线程执行。。。。。。。");
                System.out.println(Thread.currentThread());
            }
        }).start();

        FutureTask futureTask = new FutureTask(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("Callable线程执行。。。。。。。");
                System.out.println(Thread.currentThread());
                return "callable线程执行完成";
            }
        });
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

    }

    class 自定义Thread类 extends Thread{
        public 自定义Thread类(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread()+"Thread线程执行。。。。。。。");

            System.out.println("sleep开始休眠，休眠时间：1s，当前时间" + System.currentTimeMillis());
            System.out.println();
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread()+"sleep休眠结束，当前时间" + System.currentTimeMillis());

        }
    }

    @Test
    public void 线程的休眠api验证(){

        try {
            // 继承Thread类，重新run方法
            Thread thread = new 自定义Thread类("线程1");
            thread.start();

            Thread.sleep(500); // 主线程暂停0.5秒

            // 继承Thread类，重新run方法
            Thread thread2 = new 自定义Thread类("线程2");
            thread2.start();

            Thread.sleep(10000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void 定时任务创建线程验证() throws InterruptedException {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("schedule线程开始执行");
            }
        },5000);

        Thread.sleep(10000);
    }

    @Test
    public void 自定义线程池验证(){
        ThreadPoolExecutor threadPool =
                new ThreadPoolExecutor(
                        1,
                        1,
                        10,
                        TimeUnit.MINUTES,
                        new LinkedBlockingDeque<>(1),
                        Executors.defaultThreadFactory(),
                        new ThreadPoolExecutor.CallerRunsPolicy());


        threadPool.execute(new Runnable实现类());
        threadPool.execute(new Runnable实现类());
        threadPool.execute(new Runnable实现类());
        threadPool.execute(new Runnable实现类());

//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        for (int i = 0; i < 10; i++) {
//            threadPool.execute(new Runnable实现类());
//        }

    }

    class Runnable实现类 implements  Runnable{
        @Override
        public void run() {
            多线程方法();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void 多线程方法(){
        System.out.println(Thread.currentThread()+"任务开始执行");
    }


}
